package com.ruoyi.business.controller;

import java.util.List;

import com.ruoyi.business.domain.JtAppUser;
import com.ruoyi.business.domain.JtExpressDeliveryCompany;
import com.ruoyi.business.domain.JtOrderOperateLog;
import com.ruoyi.business.service.IJtAppUserService;
import com.ruoyi.business.service.IJtExpressDeliveryCompanyService;
import com.ruoyi.business.service.IJtOrderOperateLogService;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.common.utils.StringUtils;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.business.domain.JtOrder;
import com.ruoyi.business.service.IJtOrderService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

import javax.validation.constraints.NotNull;

/**
 * 订单Controller
 * 
 * @author Alan Zhang
 * @date 2025-03-29
 */
@Controller
@RequestMapping("/business/order")
public class JtOrderController extends BaseController
{
    private String prefix = "business/order";

    @Autowired
    private IJtOrderService jtOrderService;
    @Autowired
    private IJtExpressDeliveryCompanyService jtExpressDeliveryCompanyService;
    @Autowired
    private IJtAppUserService jtAppUserService;

    @RequiresPermissions("business:order:view")
    @GetMapping()
    public String order()
    {
        return prefix + "/order";
    }


    /**
     * 查询订单列表
     */
    @RequiresPermissions("business:order:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(JtOrder jtOrder)
    {
        startPage();
        List<JtOrder> list = jtOrderService.selectJtOrderList(jtOrder);
        return getDataTable(list);
    }

    //分销商 订单
    @RequiresPermissions("distributor:order:view")
    @GetMapping("/distributor")
    public String distributorOrder()
    {
        return prefix + "/distributorOrder";
    }

    /**
     * 查询订单列表
     */
    @RequiresPermissions("distributor:order:view")
    @PostMapping("/distributor/list")
    @ResponseBody
    public TableDataInfo distributorOrderList(JtOrder jtOrder)
    {
        JtAppUser param = new JtAppUser();
        param.setSysUserId(ShiroUtils.getUserId());
        List<JtAppUser> appUserList = jtAppUserService.selectJtAppUserList(param);
        if(appUserList.size() > 0){
            jtOrder.setUserId(appUserList.get(0).getUserId());
        } else {
            jtOrder.setUserId(-1L);
        }
        jtOrder.setOrderType(2);

        startPage();
        List<JtOrder> list = jtOrderService.selectJtOrderList(jtOrder);
        return getDataTable(list);
    }

    /**
     * 导出订单列表
     */
    @RequiresPermissions("business:order:export")
    @Log(title = "订单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(JtOrder jtOrder)
    {
        List<JtOrder> list = jtOrderService.selectJtOrderList(jtOrder);
        ExcelUtil<JtOrder> util = new ExcelUtil<JtOrder>(JtOrder.class);
        return util.exportExcel(list, "订单数据");
    }


    /**
     * 导出订单列表
     */
    @RequiresPermissions("business:order:export")
    @Log(title = "订单-分销商", businessType = BusinessType.EXPORT)
    @PostMapping("/distributor/export")
    @ResponseBody
    public AjaxResult distributorExport(JtOrder jtOrder)
    {
        JtAppUser param = new JtAppUser();
        param.setSysUserId(ShiroUtils.getUserId());
        List<JtAppUser> appUserList = jtAppUserService.selectJtAppUserList(param);
        if(appUserList.size() > 0){
            jtOrder.setUserId(appUserList.get(0).getUserId());
        } else {
            jtOrder.setUserId(-1L);
        }
        jtOrder.setOrderType(2);

        List<JtOrder> list = jtOrderService.selectJtOrderList(jtOrder);
        ExcelUtil<JtOrder> util = new ExcelUtil<JtOrder>(JtOrder.class);
        return util.exportExcel(list, "订单数据");
    }

    /**
     * 查看订单
     */
    @RequiresPermissions(value = {"business:order:view","distributor:order:view"},logical = Logical.OR)
    @GetMapping("/view/{orderId}")
    public String view(@PathVariable("orderId") Long orderId, ModelMap mmap)
    {
        JtOrder jtOrder = jtOrderService.selectJtOrderByOrderId(orderId);
        mmap.put("jtOrder", jtOrder);
        return prefix + "/viewOrder";
    }

    /**
     * 新增订单
     */
    @RequiresPermissions("business:order:add")
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存订单
     */
    @RequiresPermissions("business:order:add")
    @Log(title = "订单", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    @RepeatSubmit
    public AjaxResult addSave(JtOrder jtOrder)
    {
        return toAjax(jtOrderService.insertJtOrder(jtOrder));
    }

    /**
     * 修改订单
     */
    @RequiresPermissions("business:order:edit")
    @GetMapping("/edit/{orderId}")
    public String edit(@PathVariable("orderId") Long orderId, ModelMap mmap)
    {
        JtOrder jtOrder = jtOrderService.selectJtOrderByOrderId(orderId);
        mmap.put("jtOrder", jtOrder);
        return prefix + "/edit";
    }

    /**
     * 修改保存订单
     */
    @RequiresPermissions("business:order:edit")
    @Log(title = "订单", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(JtOrder jtOrder)
    {
        return toAjax(jtOrderService.editJtOrder(jtOrder));
    }

    /**
     * 删除订单
     */
    @RequiresPermissions("business:order:remove")
    @Log(title = "订单", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(jtOrderService.deleteJtOrderByOrderIds(ids));
    }

    /**
     * 订单 发货页面
     */
    @RequiresPermissions("business:order:delivery")
    @GetMapping("/delivery/{orderId}")
    public String delivery(@PathVariable("orderId") Long orderId, ModelMap mmap)
    {
        JtOrder jtOrder = jtOrderService.selectJtOrderByOrderId(orderId);
        mmap.put("jtOrder", jtOrder);

        JtExpressDeliveryCompany param = new JtExpressDeliveryCompany();
        param.setStatus(0);
        List<JtExpressDeliveryCompany> deliveryCompanyList = jtExpressDeliveryCompanyService.selectJtExpressDeliveryCompanyList(param);
        mmap.put("deliveryList", deliveryCompanyList);
        return prefix + "/delivery";
    }

    /**
     * 订单发货
     */
    @RequiresPermissions("business:order:delivery")
    @Log(title = "订单-发货", businessType = BusinessType.UPDATE)
    @PostMapping("/delivery")
    @ResponseBody
    public AjaxResult delivery(JtOrder jtOrder)
    {
        StringBuilder sbMsg = new StringBuilder();
        if(StringUtils.isEmpty(jtOrder.getLogisticsCompany())){
            sbMsg.append("快递公司不能为空！");
        }
        if(StringUtils.isEmpty(jtOrder.getTrackingNumber())){
            sbMsg.append("快递单号不能为空！");
        }
        if(sbMsg.length() > 0){
            return AjaxResult.error(sbMsg.toString());
        }

        return jtOrderService.delivery(jtOrder);
    }

    /**
     * 手工支付 订单
     */
    @RequiresPermissions("business:order:payment")
    @GetMapping("/payment/{orderId}")
    public String payment(@PathVariable("orderId") Long orderId, ModelMap mmap)
    {
        JtOrder jtOrder = jtOrderService.selectOrderInfoByOrderId(orderId);
        mmap.put("jtOrder", jtOrder);
        return prefix + "/payment";
    }

    /**
     * 订单 手工支付
     */
    @RequiresPermissions("business:order:payment")
    @Log(title = "订单-手工支付", businessType = BusinessType.UPDATE)
    @PostMapping( "/payment")
    @ResponseBody
    public AjaxResult payment(JtOrder jtOrder, @NotNull String transactionNo)
    {
        if(StringUtils.isEmpty(transactionNo)){
            return AjaxResult.error("请填写交易单号！");
        }
        if(transactionNo.length() <= 10){
            return AjaxResult.error("交易单号不能小于10位！");
        }
        return jtOrderService.manualPayment(jtOrder, transactionNo);
    }

}
